/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.tabbedproperties.section;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.views.properties.tabbed.ITabbedPropertyConstants;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage;
import org.eclipse.uml2.uml.Extension;
import org.eclipse.uml2.uml.ExtensionEnd;

import com.cea.papyrus.core.tabbedproperties.PropertyViewSection;

/**
 * 
 */
public class RequiredSection extends PropertyViewSection {


	/**
	 * 
	 */
	private Button isRequired;
	
	/**
	 * 
	 */
	private SelectionListener isRequiredListener;


	/**
	 * 
	 * 
	 * @param tabbedPropertySheetPage 
	 * @param parent 
	 */
	@Override
	public void createControls(Composite parent,
			TabbedPropertySheetPage tabbedPropertySheetPage) {

		super.createControls(parent, tabbedPropertySheetPage);
		Composite composite = getWidgetFactory()
		.createFlatFormComposite(parent);
		FormData data;

		isRequired = getWidgetFactory().createButton(composite, "", SWT.CHECK);
		data = new FormData();
		data.left = new FormAttachment(0, 85);
		data.top = new FormAttachment(0, 0);
		isRequired.setLayoutData(data);

		isRequired.addSelectionListener(isRequiredListener = new SelectionListener() {
			public void widgetSelected(SelectionEvent e) {
				if(isRequired.getSelection()) {
					((ExtensionEnd)((Extension)getElement()).getOwnedEnds().get(0)).setLower(1);
				} else {
					((ExtensionEnd)((Extension)getElement()).getOwnedEnds().get(0)).setLower(0);
				}

			}

			public void widgetDefaultSelected(SelectionEvent e){
			}
		});

		CLabel derivedLabel = getWidgetFactory().createCLabel(composite, "Required:"); //$NON-NLS-1$
		data = new FormData();
		data.left = new FormAttachment(0, 0);
		data.right = new FormAttachment(isRequired,
				-ITabbedPropertyConstants.HSPACE);
		data.top = new FormAttachment(isRequired, 0, SWT.CENTER);
		derivedLabel.setLayoutData(data);

	}

	/**
	 * 
	 */
	@Override
	public void refresh() {
		if (!isRequired.isDisposed()){
			if(getElement() instanceof Extension){
				isRequired.setSelection(((Extension)getElement()).isRequired());
			}
		}
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.core.ui.properties.tabbed.PropertyViewSection#dispose()
	 */
	/**
	 * 
	 */
	public void dispose(){
		super.dispose();
		if(isRequired!=null && !isRequired.isDisposed())
			isRequired.removeSelectionListener(isRequiredListener);
	}

}

